<?php
namespace TennisTournament\Controller;

use TennisTournament\Model\Player;
use TennisTournament\Service\PlayerService;

use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;
use Zend\Json\Json;

class ManagementController extends AbstractActionController {

    /** @var PlayerService */
    protected $playerService;

    const FORWARD_MANAGEMENT = 'tennis-tournament/management/management';

    public function __construct(PlayerService $playerService) {
        $this->playerService = $playerService;
    }

    public function managementAction() {

        $parameters = array();

        $parameters['playersJSON'] = $this->generateJSONArrayFromPlayers();

        $viewModel = new ViewModel();
        $viewModel->setTemplate(self::FORWARD_MANAGEMENT);

        return $viewModel->setVariables($parameters);
    }

    private function generateJSONArrayFromPlayers() {
        $collectionPlayers = $this->playerService->listAll();
        $collectionPlayersJSON = array();

        /** @var $player Player */
        foreach ($collectionPlayers as $player) {
            array_push($collectionPlayersJSON, $player->toArray());
        }

        return Json::encode($collectionPlayersJSON);
    }

}